<template>
  <div class="scaleDialog">
    <basic-container>
      <avue-crud
        :option="option"
        :data="data"
        :page.sync="page"
        v-model="item"
        @on-load="onLoad"
        @search-change="searchChange"
        @selection-change="selectionChange"
        :search.sync="search"
        @refresh-change="onLoad"
      >
        <template slot="menuLeft">
          <el-button
            type="primary"
            size="small"
            icon="el-icon-plus"
            @click="submitInfo"
            >添加绑定用户</el-button
          >
          <el-button size="small" icon="" @click="cancelDialog">取消</el-button>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { saveProjectgaugeincludeuse } from "@/api/census/user";
import { getList } from "@/api/system/user";
export default {
  name: "scaleDialog",
  props: ["paramsData"],
  // components: { QuestionPreview },
  data() {
    let _this = this;
    return {
      item: {},
      questionPreviewVisible: false,
      search: {},
      gaugeId: "",
      selectionList: [],
      promote: true,
      page: {
        pageSize: 10,
      },
      option: {
        addBtnText: "添加量表",
        addBtn: false,
        excelBtn: false,
        selectClearBtn: false,
        delBtn: false,
        border: true,
        index: true,
        selection: true,
        editBtn: false,
        expandLevel: 3,
        headerAlign: "left",
        align: "left",
        tree: false,
        maxHeight: 200,
        labelWidth: 100,
        menuWidth: 100,
        selectable: (row) => {
          for (let index in _this.paramsData.allData) {
            if (_this.paramsData.allData[index].includeId == row.id) {
              return false;
            }
          }
          return true;
        },
        menu: false,
        column: [
          {
            label: "用户姓名",
            prop: "realName",
            searchSpan: 10,
            search: true,
          },
          {
            label: "用户学号",
            prop: "code",
            // search: true
          },
          {
            label: "手机号码",
            prop: "phone",
            overHidden: true,
          },
          {
            label: "用户性别",
            search: true,
            searchSpan: 10,
            prop: "sex",
            type: "select",
            dicData: [
              {
                label: "男",
                value: 1,
              },
              {
                label: "女",
                value: 2,
              },
            ],
          },
          {
            label: "所属角色",
            prop: "roleId",
            type: "select",
            searchSpan: 10,
            overHidden: true,
            dicUrl: `api/blade-system/role/tree?tenantId=${_this.$route.query.tenantId}`,
            props: {
              label: "title",
              value: "id",
            },
          },
          {
            label: "所属部门",
            prop: "deptName",
            overHidden: true,
          },
          {
            label: "所属岗位",
            prop: "postName",
            type: "tree",
            overHidden: true,
          },
          // {
          //   label: "所属岗位",
          //   prop: "postId",
          //   type: "tree",
          //   overHidden: true,
          //   search: true,
          //   hide: true,
          //   searchSpan: 10,
          //   dicUrl: `/api/blade-system/post/select`,
          //   props: {
          //     label: "postName",
          //     value: "id",
          //   },
          // },
        ],
      },
      data: [],
      query: {},
      postId: "",
    };
  },
  async created() {
    this.onLoad(this.page);
  },
  methods: {
    onLoad(page, params = this.search) {
      if (!page) {
        page = this.page;
      }
      params.gradeIds = this.paramsData.gradeIds;
      this.loading = true;
      if (window.localStorage.getItem("saber-userInfo")) {
        params.tenantId = JSON.parse(
          window.localStorage.getItem("saber-userInfo")
        ).content.tenantId;
      }
      this.query.postId = this.postId;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query)
      ).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        // console.log(this.data);
        this.loading = false;
      });
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    submitInfo() {
      let projectId = this.$route.query.projectId;
      this.selectionList.map((item) => {
        item.projectId = projectId;
        item.includeName = item.realName;
        item.includeId = item.id;
        item.className = item.classNo;
        item.userCode = item.code;
        item.grade = item.gradeId;
        item.category = item.postCategory;
      });
      let params = {};
      params.projectId = projectId;
      params.projectGaugeRelationEntityList = this.selectionList;
      saveProjectgaugeincludeuse(params).then(() => {
        this.$emit("onloadInfo");
        this.$emit("close");
        this.$message({
          type: "success",
          message: "操作成功!",
        });
      });
    },
    handlePreview(row) {
      this.gaugeId = row.gaugeId;
      this.questionPreviewVisible = true;
    },
    cancelDialog() {
      this.$emit("close");
    },
    async searchChange(params, done) {
      this.postId = params.postId;
      await this.onLoad(this.page, params);
      done();
    },
  },
};
</script>

<style lang="scss" scoped>
.scaleDialog {
}
</style>
